﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _00365.Security
{
    public partial class GroupManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadGroupData();
            }

        }

        protected void btnNewGroup_Click(object sender, EventArgs e)
        {
            txtGroupName.Text = string.Empty;
            rdlActive.SelectedValue = "True";
            lblNewGroupDialogTitle.Text = "Incluir Novo Grupo";
            hidFlagAddEdit.Value = "Add";
            this.popAddEditGroup.Show();
        }

        protected void btnCloseNewGroup_Click(object sender, ImageClickEventArgs e)
        {
            popAddEditGroup.Hide();
            popGroupUsers.Hide();
        }

        protected void btnSaveNewGroup_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtGroupName.Text.Trim()))
            {
                if (hidFlagAddEdit.Value == "Edit")
                {
                    Data.UserGroup editGroup = new Data.UserGroup();
                    editGroup.Id = new Guid(hidGroupID.Value);
                    editGroup.Name = txtGroupName.Text.Trim();
                    editGroup.Active = (rdlActive.SelectedValue == "True" ? true : false);
                    Biz.UserGroups.Update(editGroup);
                    hidGroupID.Value = "";
                }
                else
                {
                    Data.UserGroup newGroup = new Data.UserGroup();
                    newGroup.Id = Guid.NewGuid();
                    newGroup.Name = txtGroupName.Text.Trim();
                    newGroup.Active = (rdlActive.SelectedValue == "True" ? true : false);
                    Biz.UserGroups.Add(newGroup);
                }
                loadGroupData();
            }
        }

        protected void loadGroupData()
        {
            this.grdGroups.DataSource = Biz.UserGroups.GetAll();
            this.grdGroups.DataBind();
        }

        protected void grdGroups_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Data.UserGroup group = null;
            if (e.CommandName != string.Empty && e.CommandArgument.ToString().Length == 36)
                group = Biz.UserGroups.Get(new Guid(e.CommandArgument.ToString()));
            
            switch (e.CommandName)
            {
                case "ViewUsers":
                    BuildGroupUsers(Biz.UserGroups.Get(group.Id));
                    hidGroupID.Value = group.Id.ToString();
                    popGroupUsers.Show();
                    break;
                case "EditGroup":
                    if (group != null)
                    {
                        lblNewGroupDialogTitle.Text = "Atualizar Informações do Grupo";
                        txtGroupName.Text = group.Name;
                        rdlActive.SelectedValue = group.Active.ToString();
                        hidGroupID.Value = group.Id.ToString();
                        hidFlagAddEdit.Value = "Edit";
                        this.popAddEditGroup.Show();
                    }
                    break;
                case "DeleteGroup":
                    hidGroupID.Value = group.Id.ToString();                    
                    popDeleteMsg.Show();                    
                    break;
                case "Permissions":
                    BuildPermissions(Biz.UserGroups.Get(group.Id));
                    hidGroupID.Value = group.Id.ToString();
                    popPermissions.Show();
                    break;
                default:
                    break;
            }
        }

        private void BuildPermissions(Data.UserGroup userGroup)
        {
            grdGranted.DataSource = Biz.Permissions.GetPermissionsFor(userGroup);
            grdGranted.DataBind();

            grdAvailableFeatures.DataSource = Biz.Features.GetAllButFrom(userGroup);
            grdAvailableFeatures.DataBind();
        }

        protected void btnDeleteMsgYes_Click(object sender, EventArgs e)
        {
            Data.UserGroup group = null;

            if (hidGroupID.Value != string.Empty && hidGroupID.Value.ToString().Length == 36)
                group = Biz.UserGroups.Get(new Guid(hidGroupID.Value.ToString()));
            if (group != null)
            {
                Biz.UserGroups.Remove(group.Id);
                loadGroupData();
            }
            popDeleteMsg.Hide();
        }

        protected void btnDeleteMsgNo_Click(object sender, EventArgs e)
        {
            popDeleteMsg.Hide();
        }

        private void BuildGroupUsers(Data.UserGroup group)
        {
            group.Logins.Load();
            grdGroupUsers.DataSource = group.Logins.ToList<Data.Login>();
            grdGroupUsers.DataBind();

            grdAvailableUsers.DataSource = Biz.Login.GetUsersButFrom(group);
            grdAvailableUsers.DataBind();
        }

        protected void grdGroups_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdGroups.PageIndex = e.NewPageIndex;
        }

        protected void grdGroups_PageIndexChanged(object sender, EventArgs e)
        {
            loadGroupData();
        }

        protected void grdGroupUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Remove":
                    Data.UserGroup group = Biz.UserGroups.Get(new Guid(hidGroupID.Value));
                    Data.Login user = Biz.Login.Get(new Guid(e.CommandArgument.ToString()));
                    Biz.UserGroups.RemoveUserFromGroup(group, user);
                    BuildGroupUsers(group);
                    popGroupUsers.Show();
                    break;                
            }
        }
        
        protected void grdAvailableUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    Data.UserGroup group = Biz.UserGroups.Get(new Guid(hidGroupID.Value));
                    Data.Login user = Biz.Login.Get(new Guid(e.CommandArgument.ToString()));
                    Biz.UserGroups.AddUserToGroup(group, user);
                    BuildGroupUsers(group);
                    popGroupUsers.Show();
                    break;                
            }
        }
        
        protected void grdGranted_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Revoke":
                    Data.UserGroup Group = Biz.UserGroups.Get(new Guid(hidGroupID.Value));
                    Data.Feature Feature = Biz.Features.Get(new Guid(e.CommandArgument.ToString()));
                    Biz.Permissions.RevokePermissionFor(Feature, Group);
                    BuildPermissions(Biz.UserGroups.Get(new Guid(hidGroupID.Value)));
                    popPermissions.Show();
                    break;
            }
        }

        protected void grdAvailableFeatures_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Grant":
                    Data.Feature feat = Biz.Features.Get(new Guid(e.CommandArgument.ToString()));
                    Data.UserGroup group = Biz.UserGroups.Get(new Guid(hidGroupID.Value));

                    Biz.Permissions.GrantPermissionFor(feat, group);
                    BuildPermissions(Biz.UserGroups.Get(new Guid(hidGroupID.Value)));
                    popPermissions.Show();
                    break;
            }
        }
            
    }
}